<?php

namespace app\admin\controller\sys;
use app\admin\controller\Base;
use think\facade\Request;
use think\facade\View;

use app\admin\model\sys\AuthGroup;

class Admin extends Base
{
    protected $model;
    public function initialize()
    {
        parent::initialize();
        $this->model = new \app\admin\model\sys\Admin;
        $this->validate = new \app\admin\validate\sys\Admin();
        $this->redict = url('index');
    }
    public function index(){
        if (Request::isPost()) {
            $keys = $this->request->post('keys', '', 'trim');
            $option = [
                'where' => [
                    ['username|email|mobile','like',"%".$keys."%"]
                ],
                'with' => ['groups'], // 关联的模型名称
                'visible' => [
                    'groups' => ['title']  // 关联的模型名称要显示的字段名，默认展示全部
                ]
            ];
            $list = $this->model->loadList($option);
            if ($list['result'] == 'success'){
                $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => $list['list'], 'count' => $list['page']['total']];
            }else{
                $result = ['code' => 0, 'msg' => lang('get info success'), 'data' => [], 'count' => 0];
            }
            return $result;
        }
        return view();
    }
    // 管理员添加
    public function add()
    {
        if (Request::isPost()) {
            $this->scene = 'add';
            return parent::create();
        }else{
            $info = '';
            $auth_group = AuthGroup::where('status', 1)->select();
            $view = [
                'info'  =>$info,
                'authGroup' => $auth_group,
                'title' => lang('add'),
            ];
            View::assign($view);
            return view('form');
        }
    }
    /**
     * 管理员修改
     */
    public function edit()
    {
        if (Request::isPost()) {
            $this->scene = 'edit';
            return parent::modify();
        } else {
            $id = Request::param('id');
            if ($id) {
                $auth_group = AuthGroup::select();
                $admin = $this->model->find($id);
                unset($admin['password']);
                $view = [
                    'info' => $admin,
                    'authGroup' => $auth_group,
                    'title' => lang('edit'),
                ];
                View::assign($view);
                return view('form');
            }
        }
    }

}